﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinqToPi;
using System.IO;
using System.Data.Common;
using System.Data.OleDb;
using LinqToPi.Data;


namespace LinqToPi
{
    public class PiServerContext : PiContext
    {
        public PiArchiveContext Archive { get; set; }
        public PiPointContext Point { get; set; }
        public PiSystemContext System { get; set; }
        public PiUserContext User { get; set; }

       


        #region Constructors

      

        public PiServerContext(string servername, bool UseIntegratedSecurity,  TextWriter log)
            : this(new OleDbConnection(UseIntegratedSecurity 
                ? string.Format("Provider = {0} ; Data Source= {1} ; Integrated Security = SSPI",
                                                                            PiOleDBProvider,
                                                                            servername
                                                                          )
                : string.Format("Provider = {0} ; Data Source= {1} ;",
                                                                            PiOleDBProvider,
                                                                            servername
                                                                          )), 
                                       log)
        {               
        }

        public PiServerContext(string servername, string username, TextWriter log)
            : this(new OleDbConnection(string.Format("Provider = {0} ; Data Source= {1} ; User ID = {2} ; ",
                                                                          PiOleDBProvider,
                                                                          servername,
                                                                          username
                                                                          )),
                                        log)
            
        {
        }

        public PiServerContext(string servername, string username, string password, TextWriter log)
            : this(new OleDbConnection(string.Format("Provider = {0} ; Data Source= {1} ; User ID = {2} ; Password = {3} ; ",
                                                                            PiOleDBProvider,
                                                                            servername,
                                                                            username,
                                                                            password
                                                                            )),
                                        log)
        {
        }

        private PiServerContext(DbConnection connection, TextWriter log)
            : this(new DbQueryProvider(connection, StandardPolicy, log))
        {
            connection.Open();
        }

        public PiServerContext(IQueryProvider provider) 
        {
            Archive = new PiArchiveContext(provider);
            Point = new PiPointContext(provider);
            System = new PiSystemContext(provider);
            User = new PiUserContext(provider);
        }

        #endregion
    }
}
